//获取应用实例
var app = getApp()
Page({

  data: {
    payWay:'1',
  },
  onLoad: function (e) {
    var that = this;
    if(e){
       that.setData({
         cardid: e.cardid,
         storeid: e.storeid,
         price:e.price
       })
    }
    that.getCurrentDate();
    that.getUserInfor();
    console.log(app.globalData.userInfo);
  },
  payRadioChange: function (e) {
    var that = this;
    that.setData({
      payWay: e.detail.value
    })
    console.log(that.data.payWay);
  },
  toRecharge: function () {
    wx.navigateTo({
      url: '/subpages/walletRecharge/walletRecharge',
    })
  },
  //获得当前的日期
  getCurrentDate:function(){
    var that = this;
    var myDate = new Date();
    //获取当前年
    var year = myDate.getFullYear();
    //获取当前月
    var month = myDate.getMonth() + 1;
    //获取当前日
    var day = myDate.getDate(); 
    var str = year+'-'+month+'-'+day;
    that.setData({
      date: str
    });
    console.log(that.data.date);
  },
  //获得所填信息
  getUserInfor:function(){
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_store_member_info',
        type: 1
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          that.setData({
            name:res.data.data.name, 
            sexValue: res.data.data.gender ? res.data.data.gender : '1',
            date: res.data.data.birthday,
            telphone: res.data.data.telphone,
            coin: parseFloat(res.data.data.coin) 
          })
        } else {
          console.log(res.data)
        }
      },
      complete: function () {
      }
    });
  },
  //获得生日
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  //获得支付方式
  radioChange: function (e) {
  var that = this;
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    that.setData({
      payType:e.detail.value
    })
  },
  sexSelect:function(e){
    var that = this;
    var sex = e.currentTarget.dataset.sex;
    that.setData({
      sexValue:sex
    })
  },
  nameChange:function(e){
    var that = this;
    that.setData({
      name:e.detail.value
    })
    console.log(that.data.name);
  },
  phoneChange: function (e) {
    var that = this;
    console.log('电话');
    console.log(e.detail.value);
    that.setData({
      telphone: e.detail.value
    })
    console.log(that.data.telphone);
  }, 
  submitInfor: function () {
    var that = this;
    app.getSubId(that, 'applet_buy_member_card', 'toSubmitInfor');
  },
  toSubmitInfor:function(){
    var that = this;
    var data = {};
    var cardid = that.data.cardid;
    var type = that.data.type;
    var name = that.data.name;
    var gender  = that.data.sexValue;
    var birthday = that.data.date;
    var telphone = that.data.telphone;
    data.map = 'applet_buy_member_card';
    data.cardid = cardid;
    data.storeid = that.data.storeid;
    data.name = name;
    data.gender = gender;
    data.birthday = birthday;
    data.telphone = telphone;
    data.payType = that.data.payWay;
    if (!data.telphone) {
      app.errorTip(that, "请输入手机号码", 2000);
      return;
    }
    console.log(data);
    if(that.data.price>0){
      wx.showModal({
        title: '提示',
        content: '确认立即支付吗？',
        success(result) {
          if (result.confirm) {
            that.gotopay(data);
          } else if (result.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    }else{
      that.gotopay(data);
    }
  },
  gotopay:function(data){
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          if (res.data.data.status == 'zfcg') {
            app.errorTip(that, '开卡成功！', 2000);
            setTimeout(function () {
              wx.navigateBack({
                delta: 3
              })
            }, 1000)
          } else if (res.data.data.status == 'dzf') {
            that.orderPay(res.data.data.tid);
            // wx.setStorage({
            //   key: 'cardOrderInfo',
            //   data: res.data.data,
            //   success: function () {
            //     wx.navigateTo({
            //       url: '/subpages/cardOrderPay/cardOrderPay',
            //     })
            //   }
            // })
          }
        } else {
          console.log(res.data);
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  orderPay: function (tid) {
    var that = this;
    var data = {};
    data.map = 'applet_pay_member_card';
    data.tid = tid;
    console.log(data);
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 1000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              // wx.redirectTo({
              //   url: '/pages/cardPaySuccess/cardPaySuccess?orderid=' + data.tid
              // })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 3
                })
              }, 100)
            },
            'fail': function (res) {
              console.log(res);
            }
          });
        } else {
          console.log(res.data);
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
})